home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12799 < prev    next >
Encoding:
Text File  |  1996-08-05  |  9.9 KB  |  287 lines

  1. Path: howland.reston.ans.net!psinntp!psinntp!psinntp!pipeline!not-for-mail
  2. From: johndill@nyc.pipeline.com (John Dillworth)
  3. Newsgroups: comp.lang.c,
  4. Subject: Win 3.1 SDK Why does this lock up??
  5. Date: 2 Apr 1996 16:31:24 -0500
  6. Organization: Pipeline
  7. Message-ID: <4js6bc$qi9@pipe9.nyc.pipeline.com>
  8. NNTP-Posting-Host: pipe9.nyc.pipeline.com
  9. X-PipeUser: johndill
  10. X-PipeHub: nyc.pipeline.com
  11. X-PipeGCOS: (John Dillworth)
  12. X-Newsreader: Pipeline v3.5.0
  13.  
  14. Sorry about the lenght of this code.  This is a modifaction of one of the
  15. Petzold Win 3.1 programs.  I want this progrm to work like this.  When you
  16. move the sliders the color int the tp part of the client are should change
  17. accordingly.  No problem with that.  I added a window below that.  I want
  18. this window to display the color of the slider that has the focus.  The
  19. progam comes up correctly but locks up.  I think the problem is in the
  20. paint messege 
  21. suggestions? 
  22.  
  23.  
  24. #include <windows.h> 
  25. #include <stdlib.h> 
  26.  
  27. #define min(a,b) (((a) < (b)) ? (a) : (b)) 
  28. #define max(a,b) (((a) > (b)) ? (a) : (b)) 
  29.  
  30. long FAR PASCAL _export WndProc    (HWND, UINT, UINT, LONG) ; 
  31. long FAR PASCAL _export ScrollProc (HWND, UINT, UINT, LONG) ; 
  32.  
  33. FARPROC lpfnOldScr[3] ; 
  34. HWND    hwndScrol[3], hwndLabel[3], hwndValue[3], hwndRect, hwndRect2 ; 
  35. short   color[3], nFocus ; 
  36.  
  37. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance, 
  38.                     LPSTR lpszCmdLine, int nCmdShow) 
  39.      { 
  40.      static char szAppName[] = "Colors1" ; 
  41.      static char *szColorLabel[] = { "Red", "Green", "Blue" } ; 
  42.      FARPROC     lpfnScrollProc ; 
  43.      HWND        hwnd ; 
  44.      MSG         msg; 
  45.      short       n ; 
  46.      WNDCLASS    wndclass ; 
  47.  
  48.      if (hPrevInstance) 
  49.           return FALSE ; 
  50.  
  51.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ; 
  52.      wndclass.lpfnWndProc   = WndProc ; 
  53.      wndclass.cbClsExtra    = 0 ; 
  54.      wndclass.cbWndExtra    = 0 ; 
  55.      wndclass.hInstance     = hInstance ; 
  56.      wndclass.hIcon         = NULL ; 
  57.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ; 
  58.      wndclass.hbrBackground = CreateSolidBrush (0L) ; 
  59.      wndclass.lpszMenuName  = NULL ; 
  60.      wndclass.lpszClassName = szAppName ; 
  61.  
  62.      RegisterClass (&wndclass) ; 
  63.  
  64.      hwnd = CreateWindow (szAppName, "Color Scroll", 
  65.                           WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN, 
  66.                           CW_USEDEFAULT, CW_USEDEFAULT, 
  67.                           CW_USEDEFAULT, CW_USEDEFAULT, 
  68.                           NULL, NULL, hInstance, NULL) ;  
  69.                            
  70.                            
  71.  
  72.      hwndRect = CreateWindow ("static", NULL, 
  73.                               WS_CHILD | WS_VISIBLE | SS_WHITERECT, 
  74.                               0, 0, 0, 0, 
  75.                               hwnd, 9, hInstance, NULL) ; 
  76.      hwndRect2 = CreateWindow ("static", NULL, 
  77.                               WS_CHILD | WS_VISIBLE | SS_WHITERECT, 
  78.                               0, 0, 0, 0, 
  79.                               hwnd, 9, hInstance, NULL) ;  
  80.                                 
  81.                                
  82.  
  83.      lpfnScrollProc = MakeProcInstance ((FARPROC) ScrollProc, hInstance) ; 
  84.  
  85.      for (n = 0 ; n < 3 ; n++)  
  86.           { 
  87.           hwndScrol[n] = CreateWindow ("scrollbar", NULL, 
  88.                               WS_CHILD | WS_VISIBLE | WS_TABSTOP |
  89. SBS_VERT, 
  90.                               0, 0, 0, 0, 
  91.                               hwnd, n, hInstance, NULL) ; 
  92.  
  93.           hwndLabel[n] = CreateWindow ("static", szColorLabel[n], 
  94.                               WS_CHILD | WS_VISIBLE | SS_CENTER, 
  95.                               0, 0, 0, 0, 
  96.                               hwnd, n + 3, hInstance, NULL) ; 
  97.  
  98.           hwndValue[n] = CreateWindow ("static", "0", 
  99.                               WS_CHILD | WS_VISIBLE | SS_CENTER, 
  100.                               0, 0, 0, 0, 
  101.                               hwnd, n + 6, hInstance, NULL) ;  
  102.  
  103.           lpfnOldScr[n] = (FARPROC) GetWindowLong (hwndScrol[n],
  104. GWL_WNDPROC) ; 
  105.           SetWindowLong (hwndScrol[n], GWL_WNDPROC, (LONG) lpfnScrollProc)
  106.  
  107.           SetScrollRange (hwndScrol[n], SB_CTL, 0, 255, FALSE) ; 
  108.           SetScrollPos   (hwndScrol[n], SB_CTL, 0, FALSE) ; 
  109.           } 
  110.  
  111.      ShowWindow (hwnd, nCmdShow) ; 
  112.      UpdateWindow (hwnd); 
  113.  
  114.      while (GetMessage (&msg, NULL, 0, 0)) 
  115.           { 
  116.           TranslateMessage (&msg) ; 
  117.           DispatchMessage  (&msg) ; 
  118.           } 
  119.      return msg.wParam ; 
  120.      } 
  121.  
  122. long FAR PASCAL _export WndProc (HWND hwnd, UINT message, UINT wParam, 
  123.                                                           LONG lParam) 
  124.      { 
  125.      static HBRUSH hBrush[3] ; 
  126.      char          szbuffer[10] ; 
  127.      HDC           hdc ; 
  128.      POINT         point ; 
  129.      short         n, cxClient, cyClient, cyChar ; 
  130.      TEXTMETRIC    tm ; 
  131.   //   HDC         hdc ; 
  132.      PAINTSTRUCT ps ; 
  133.     // RECT     rect ; 
  134.  
  135.      switch (message) 
  136.           { 
  137.           case WM_CREATE : 
  138.                hBrush[0] = CreateSolidBrush (RGB (255, 0, 0)) ; 
  139.                hBrush[1] = CreateSolidBrush (RGB (0, 255, 0)) ; 
  140.                hBrush[2] = CreateSolidBrush (RGB (0, 0, 255)) ; 
  141.                return 0 ; 
  142.  
  143.           case WM_SIZE : 
  144.                cxClient = LOWORD (lParam) ; 
  145.                cyClient = HIWORD (lParam) ; 
  146.  
  147.                hdc = GetDC (hwnd) ; 
  148.                GetTextMetrics (hdc, &tm) ; 
  149.                cyChar = tm.tmHeight ; 
  150.                ReleaseDC (hwnd, hdc) ; 
  151.  
  152.                MoveWindow (hwndRect, 0, 0, cxClient /2, cyClient, TRUE) ;  
  153.  
  154.                MoveWindow (hwndRect2, cxClient/ 2,cyClient/2 , cxClient/ 2 
  155. , cyClient/2, TRUE) ;  
  156.                for (n = 0 ; n < 3 ; n++) 
  157.                     { 
  158.                     MoveWindow (hwndScrol[n], 
  159.                          (2 * n + 1) * cxClient / 14, 2 * cyChar, 
  160.                          cxClient / 14, cyClient - 4 * cyChar, TRUE) ; 
  161.  
  162.                     MoveWindow (hwndLabel[n], 
  163.                          (4 * n + 1) * cxClient / 28, cyChar / 2, 
  164.                          cxClient / 7, cyChar, TRUE) ; 
  165.  
  166.                     MoveWindow (hwndValue[n], 
  167.                          (4 * n + 1) * cxClient / 28, cyClient - 3 * cyChar
  168. / 2, 
  169.                          cxClient / 7, cyChar, TRUE) ; 
  170.                     } 
  171.                SetFocus (hwnd) ; 
  172.                return 0 ; 
  173.           case WM_PAINT:                         //PROBLEM HERE 
  174.            InvalidateRect(hwndRect2,NULL,TRUE); 
  175.            hdc = BeginPaint (hwndRect2, &ps) ; 
  176.            FillRect(hdc,&ps.rcPaint,hBrush[nFocus]); 
  177.                 
  178.            EndPaint (hwndRect2, &ps) ; 
  179.                return 0 ; 
  180.  
  181.           case WM_SETFOCUS: 
  182.                SetFocus (hwndScrol[nFocus]) ; 
  183.                return 0 ; 
  184.  
  185.           case WM_VSCROLL : 
  186.                n = GetWindowWord (HIWORD (lParam), GWW_ID) ; 
  187.  
  188.                switch (wParam) 
  189.                     { 
  190.                     case SB_PAGEDOWN : 
  191.                          color[n] += 15 ; 
  192.                                                   // fall through 
  193.                     case SB_LINEDOWN : 
  194.                          color[n] = min (255, color[n] + 1) ; 
  195.                          break ; 
  196.  
  197.                     case SB_PAGEUP : 
  198.                          color[n] -= 15 ; 
  199.                                                   // fall through 
  200.                     case SB_LINEUP : 
  201.                          color[n] = max (0, color[n] - 1) ; 
  202.                          break ; 
  203.  
  204.                     case SB_TOP: 
  205.                          color[n] = 0 ; 
  206.                          break ; 
  207.  
  208.                     case SB_BOTTOM : 
  209.                          color[n] = 255 ; 
  210.                          break ; 
  211.  
  212.                     case SB_THUMBPOSITION : 
  213.                     case SB_THUMBTRACK : 
  214.                          color[n] = LOWORD (lParam) ; 
  215.                          break ; 
  216.  
  217.                     default : 
  218.                          break ; 
  219.                     } 
  220.                SetScrollPos  (hwndScrol[n], SB_CTL, color[n], TRUE) ; 
  221.                SetWindowText (hwndValue[n], itoa (color[n], szbuffer, 10))
  222.  
  223.                DeleteObject ( 
  224.                     SetClassWord (hwnd, GCW_HBRBACKGROUND,      // passes
  225. back old brush handle 
  226.                          CreateSolidBrush ( 
  227.                               RGB (color[0], color[1], color[2])))) ; 
  228.  
  229.                InvalidateRect (hwnd, NULL, TRUE) ; 
  230.                 
  231.                return 0 ; 
  232.  
  233.           case WM_CTLCOLOR: 
  234.                if (HIWORD (lParam) == CTLCOLOR_SCROLLBAR) 
  235.                     { 
  236.                     SetBkColor (wParam, GetSysColor (COLOR_CAPTIONTEXT)) ; 
  237.                     SetTextColor (wParam, GetSysColor (COLOR_WINDOWFRAME))
  238.  
  239.                     n = GetWindowWord (LOWORD (lParam), GWW_ID) ; 
  240.                     point.x = point.y = 0 ; 
  241.                     ClientToScreen (hwnd, &point) ; 
  242.                     UnrealizeObject (hBrush[n]) ; 
  243.                     SetBrushOrg (wParam, point.x, point.y) ; 
  244.                     return ((DWORD) hBrush[n]) ; 
  245.                     } 
  246.                break ; 
  247.  
  248.           case WM_DESTROY: 
  249.                DeleteObject ( 
  250.                     SetClassWord (hwnd, GCW_HBRBACKGROUND, 
  251.                          GetStockObject (WHITE_BRUSH))) ; 
  252.  
  253.                for (n = 0 ; n < 3 ; DeleteObject (hBrush [n++])) ; 
  254.  
  255.                PostQuitMessage (0) ; 
  256.                return 0 ; 
  257.           } 
  258.      return DefWindowProc (hwnd, message, wParam, lParam) ; 
  259.      } 
  260.  
  261. long FAR PASCAL _export ScrollProc (HWND hwnd, UINT message, UINT wParam, 
  262.                                                              LONG lParam) 
  263.      { 
  264.      short n = GetWindowWord (hwnd, GWW_ID) ; 
  265.  
  266.      switch (message) 
  267.           { 
  268.           case WM_KEYDOWN: 
  269.                if (wParam == VK_TAB) 
  270.                     SetFocus (hwndScrol[(n + 
  271.                          (GetKeyState (VK_SHIFT) < 0 ? 2 : 1)) % 3]) ; 
  272.                break ; 
  273.  
  274.           case WM_SETFOCUS: 
  275.                nFocus = n ; 
  276.                break ; 
  277.           } 
  278.      return CallWindowProc (lpfnOldScr[n], hwnd, message, wParam, lParam) ;
  279.  
  280. -- 
  281. -- 
  282.  
  283. John Dillworth
  284.